package com.lbd.system.service.impl;

import com.lbd.base.service.BaseServiceImpl;
import com.lbd.system.bean.Role;
import com.lbd.system.dao.RoleRepository;
import com.lbd.system.service.RoleService;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * Created by zzl on 2016/9/26.
 */
@Service
@Transactional
public class RoleServiceImpl extends BaseServiceImpl<Role, RoleRepository> implements RoleService {
    private final Logger log = Logger.getLogger(this.getClass());
    @Autowired
    RoleRepository roleRepository;


    @Override
    public Specification<Role> getSpecification(Role role) {
        return null;
    }

    /**
     * 构建分页查询条件和参数
     *
     * @param role
     * @param parameter name
     * @return
     */
    @Override
    public String buildJpql(Role role, Map<String, Object> parameter) {
        StringBuilder jpqlBuilder = new StringBuilder(" from Role t ");
        if (role != null) {
            if (StringUtils.isNotBlank(role.getName())) {
                jpqlBuilder.append(" and t.name like :name");
                parameter.put("name", "%" + role.getName() + "%");
            }
        }
        return jpqlBuilder.toString();
    }

    @Override
    public Role findById(String id) {
        return roleRepository.findOne(id);
    }
}
